// index.js
import request from '../../utils/request.js'
import PubSub from 'pubsub-js'
// 获取应用实例
const app = getApp()

Page({
    data: {
        bannerList: [],
        recommendList: [],
        topList: []
    },

    async onLoad() {
        // 获取轮播数据
        let bannerListData = await request('/banner', {
            type: 2
        })
        this.setData({
            bannerList: bannerListData.banners
        });

        // 获取推荐歌曲数据
        let recommendListData = await request('/personalized', {
            limit: 10
        })
        this.setData({
            recommendList: recommendListData.result
        })

        // 获取排行榜数据
        // let index = 0
        // let resultArr = []
        // while (index < 5) {
        //   let topListData = await request('3000/top/list', {idx: index++})
        //   let topListItem = {name: topListData.playlist.name, tracks: topListData.playlist.tracks.slice(0,3)}
        //   resultArr.push(topListItem)
        //   this.setData({
        //     topList: resultArr
        //   })
        // } 
        /*
         * /toplist 获取所有排行榜单
         * detailList 歌单详情
         * */
        let allTopListData = await request('/toplist')
        let topList = allTopListData.list.slice(0, 5)
        let topListDetail = []
        for (let item of topList) {
            let detailList = await request(`/playlist/detail?id=${item.id}`, {
                limit: 10
            })
            topListDetail.push({
                name: detailList.playlist.name,
                tracks: detailList.playlist.tracks.slice(0, 3)
            })
            this.setData({
                topList: topListDetail
            })
        }
    },
    // 跳转每日推荐歌单
    toRecommendSong() {
        wx.navigateTo({
          url: '/pages/recommendSong/recommendSong',
        })
    },
    // 跳转歌曲详情   
    toSongDetail(event) {
        // console.log (event)
        let song = event.currentTarget.dataset.song
        // 路由跳转传参 query
        wx.navigateTo({
            url: '/pages/songDetail/songDetail?musicId='+ song.id,
        })
    },

    onReady() {

    },

    onshow() {

    },

    onHide() {

    },

    onUnload() {

    }
})